package com.yunyi.config;

import com.yunyi.handler.DbChatMemoryHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.memory.repository.jdbc.JdbcChatMemoryRepository;
import org.springframework.ai.chat.memory.repository.jdbc.MysqlChatMemoryRepositoryDialect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@RequiredArgsConstructor
public class ChatConfig {


    private final DataSource dataSource;

    private final DbChatMemoryHandler dbChatMemoryHandler;

//    @Bean
//    public ChatMemory getChatMemory() {
//        return MessageWindowChatMemory.builder().build();
//    }


    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        JdbcChatMemoryRepository springAiJdbcChatMemory = JdbcChatMemoryRepository.builder()
                .dialect(new MysqlChatMemoryRepositoryDialect())
                .dataSource(dataSource)
                .build();

        MessageWindowChatMemory mes = MessageWindowChatMemory.builder()
                .chatMemoryRepository(springAiJdbcChatMemory)
                .build();

        ChatClient build = builder.defaultSystem("你是云翳的IT智能小助手,擅长解决各种各样的问题")
                .defaultAdvisors(new SimpleLoggerAdvisor(),
                        MessageChatMemoryAdvisor.builder(mes).build())
                .build();
        return build;
    }

}
